<?php

namespace app\supplier\controller\statistics;

use app\common\model\supplier\SupplierCommissionWithdrawal;
use app\supplier\controller\Controller;
use app\supplier\service\statistics\SupplierService;
use app\common\model\supplier\SupplierCommission;
use app\common\model\supplier\CommissionConfig;
/**
 * 供应商数据控制器
 */
class Supplier extends Controller
{
    /**
     * 会员数据统计
     */
    public function index()
    {
        return $this->renderSuccess('', [
            // 供应商统计
            'supplier' => (new SupplierService())->getData(),
        ]);
    }

    /**
     * 通过时间段查询
     * $type类型：new refund
     */
    public function data($search_time)
    {
        $days = $this->getDays($search_time);
        $data = (new SupplierService())->getDataByDate($days);
        return $this->renderSuccess('', [
            // 日期
            'days' => $days,
            // 数据
            'data' => $data,
        ]);
    }

    /**
     * 获取具体日期数组
     */
    private function getDays($search_time)
    {
        //搜索时间段
        if(!isset($search_time) || empty($search_time)){
            //没有传，则默认为最近7天
            $end_time = date('Y-m-d', time());
            $start_time = date('Y-m-d', strtotime('-7 day',time()));
        }else{
            $start_time = array_shift($search_time);
            $end_time = array_pop($search_time);
        }

        $dt_start = strtotime($start_time);
        $dt_end = strtotime($end_time);
        $date = [];
        $date[] = date('Y-m-d', strtotime($start_time));
        while($dt_start < $dt_end) {
            $date[] = date('Y-m-d', strtotime('+1 day',$dt_start));
            $dt_start = strtotime('+1 day',$dt_start);
        }
        return $date;
    }

    public  function getSupplierCommissionList(){
        $params = $this->postData();
        $shop_supplier_id = $this->getSupplierId();
        $data = SupplierCommission::getSupplierCommissionList($shop_supplier_id,$params);
        return $this->renderSuccess('', compact('data'));
    }

    public function exportCommission()
    {
        $params = $this->postData();
        $shop_supplier_id = $this->getSupplierId();
        $data = SupplierCommission::getSupplierCommissionList($shop_supplier_id,$params,3);
        $list = $data['list'];
        foreach ($list as $key => $value){
            foreach ($value['product'] as $k => $v){
                $list[$key]['product_name'] = $v['product_name'];
            }
            $list[$key]['supplier_name'] = $value['user']['nickName']. "\r\n".$value['user']['user_id'];
        }
        $xls_cell = [
            ['order_no' , '订单号'],
            ['order_create_time_text' , '下单时间'],
//            ['sales_actual' , '商品名称'],
//            ['sales_actual' , '商品价格'],
//            ['product_price' , '数量'],
            ['total_price' , '订单总额'],
//            ['pay_price' , '优惠券抵扣'],
            ['pay_price' , '实付金额'],
            ['commission' , '佣金'],
            ['fee' , '支付费率(0.6%;元)'],
            ['refund_money' , '退款'],
            ['supplier_name' , '买家'],
//            ['supplier_name' , '买家昵称'],
//            ['product_stock' , '分佣比例'],


        ];
        $exp_title = '佣金明细' . date('Y-m-d H:i:s');
        exportExcel($exp_title, $xls_cell, $list);
    }
//
//   public function withdrawal(){
//       $params = $this->postData();
//       $shop_supplier_id = $params['shop_supplier_id'];
//       $money =  $params['money']??0;
//       //可提现金额
//       $balance = SupplierCommission::getWithdrawalMoney($shop_supplier_id);
//       if($money <= 0){
//           return $this->renderError('请输入提现金额');
//       }
//       if($balance < $money){
//           return $this->renderError('可提现金额不足');
//       }
//       $jjjshop_store_multi = session('jjjshop_store_multi');
//       $shop_user_id = $jjjshop_store_multi['user']['shop_user_id']??0;
//       $data['shop_supplier_id'] = $shop_supplier_id;
//       $data['shop_user_id'] = $shop_user_id;
//       $data['money'] = $money;
//       $data['status'] = 1;
//       if(SupplierCommissionWithdrawal::createData($data)){
//           return $this->renderSuccess('提现成功');
//       }else{
//           return $this->renderError('提现失败');
//       }
//
//   }

   public function getWithdrawalList(){
      $params = $this->postData();
      $params['shop_supplier_id'] = $this->getSupplierId();
      $list = SupplierCommissionWithdrawal::getWithdrawalList($params);
      return $this->renderSuccess('', compact('list'));
   }


    /**
     *提现
     * withdrawal
     * \think\response\Json
     * @return \think\response\Json
     * @Author kevin
     * @date 2024/5/8 16:43
     */
    public function withdrawal(){
        $params = $this->postData();
        $shop_supplier_id = $this->getSupplierId();;
        $money =  $params['money']??0;
        $shop_user_id = 0;
        $res = (new SupplierCommission()) -> withdrawal($shop_supplier_id,$money,$shop_user_id,0);
        if($res['status'] == 1){
            return $this->renderSuccess($res['msg']);
        }else{
            return $this->renderError($res['msg']);
        }


    }

    /**
     *获取时间列表
     * getDateList
     * \think\response\Json
     * @return \think\response\Json
     * @Author kevin
     * @date 2024/4/25 11:35
     */
   public function getDateList()
   {
       $list = [];
       for ($i=0;$i<3;$i++){
           if($i==0){
               $v['date_time_text'] = date('Y-m-d');

           }else{
               $v['date_time_text'] = date('Y-m-d', strtotime('-'.$i.' day'));
           }
           $array = [$v['date_time_text'],$v['date_time_text']];
           $v['date_time'] = $array;
           $list[$i] = $v;

       }
       return $this->renderSuccess('', compact('list'));
   }

    /**
     *获取分佣配置列表
     * getCommissionConfigList
     * void
     * @return void
     * @Author kevin
     * @date 2024/4/25 11:36
     */
    public function getCommissionConfigList(){
        $list = (new CommissionConfig()) -> select();
        foreach ($list as $key => $value){
            if($value['type']==1){
                if($value['end_moeny'] > 0){
                    $type_text = intval($value['init_money']) .'-'.intval($value['end_moeny']) .'/瓶';
                }else{
                    $type_text = intval($value['init_money']) .'以上/瓶';
                }

                $list[$key]['rate_text'] = $value['title'] . '  ['.$value['rate'].'%]['.$type_text.']';
            }else{
                $list[$key]['rate_text'] = $value['title'] . '  ['.$value['rate'].'%]';
            }

        }
        return $this->renderSuccess('', compact('list'));
    }



}